using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CE.Global;
using CE.Provider;

public partial class Admin_provider_AddNewProviderCompany : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            DataTable _dtProviderType = General.GetComboItem("TH.ProviderType", false);
            cblProviderType.DataSource = _dtProviderType;
            cblProviderType.DataTextField = _dtProviderType.Columns[0].ColumnName.ToString();
            cblProviderType.DataValueField = _dtProviderType.Columns[1].ColumnName.ToString();
            cblProviderType.DataBind();

            btnSetHex.OnClientClick = "SetHex(document.getElementById('" + txtHex.ClientID + "').value);return false;";
            btnSetRGB.OnClientClick = "ChangeColors(document.getElementById('" + txtRed.ClientID + "').value,document.getElementById('" + txtGreen.ClientID + "').value,document.getElementById('" + txtBlue.ClientID + "').value);return false;";
            txtRed.Attributes.Add("onblur", "ChangeColors(document.getElementById('" + txtRed.ClientID + "').value,document.getElementById('" + txtGreen.ClientID + "').value,document.getElementById('" + txtBlue.ClientID + "').value);return false;");
            txtGreen.Attributes.Add("onblur", "ChangeColors(document.getElementById('" + txtRed.ClientID + "').value,document.getElementById('" + txtGreen.ClientID + "').value,document.getElementById('" + txtBlue.ClientID + "').value);return false;");
            txtBlue.Attributes.Add("onblur", "ChangeColors(document.getElementById('" + txtRed.ClientID + "').value,document.getElementById('" + txtGreen.ClientID + "').value,document.getElementById('" + txtBlue.ClientID + "').value);return false;");
            //txtHex.Attributes.Add("onchange", "javascript:return SetHex(document.getElementById('" + txtHex.ClientID + "').value);");
            txtHex.Attributes.Add("onblur", "javascript:var txtHex = document.getElementById('" + txtHex.ClientID + "'); if (!SetHex(txtHex.value)){ setTimeout(\"document.getElementById(\'" + txtHex.ClientID + "\').focus();\",100); return false;}");
            if (Request.Params["comname"] != null)
            {
                GetCompanyInformation(Request.Params["comname"].ToString());
            }
            else if (Request.Params["ProviderCompanyId"] != null)
            {
                Int64 _companyId = 0;
                if (Int64.TryParse(Request.Params["ProviderCompanyId"], out _companyId))
                {
                    if (_companyId != null && _companyId > 0)
                    {
                        GetCompanyInformationById(_companyId);
                    }
                }
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Company _company = new Company();
            CompanyDB _companydb = new CompanyDB();
            bool _updatflag = false;
            if (lblProviderCompanyId.Text.Trim() != string.Empty)
            {
                _company.ProviderCompanyId = Convert.ToInt64(lblProviderCompanyId.Text);
                if (_company.ProviderCompanyId > 0)
                {
                    _updatflag = true;
                }
            }
            else
            {
                if (txtCompanyName.Text.Trim() != null)
                {
                    if (_companydb.CheckCompanyName(txtCompanyName.Text) > 0)
                    {
                        //btnAddNew.Text = "Update";
                        lblMessage.Text = "Company Name already exists!<br/>If you want to update, press update button.<br/>";
                        lblMessage.Text = "<OR> Click search button to view existing company information.";
                        return;
                    }
                }
            }

            _company.CompanyName = txtCompanyName.Text;
            _company.BusIndType = ddlBusIndType.Text;
            _company.CompanyType = ddlCompanyType.Text;

            _company.PhoneNo1 = txtPhoneNo1.Text;
            _company.PhoneNo2 = txtPhoneNo2.Text;
            _company.FaxNo1 = txtFaxNo1.Text;
            _company.FaxNo2 = txtFaxNo2.Text;
            _company.Email1 = txtEmail1.Text;
            _company.Email2 = txtEmail2.Text;
            _company.URL1 = txtURL1.Text;
            _company.URL2 = txtURL2.Text;
            _company.UnitNo1 = txtUnitNo1.Text;
            _company.UnitNo2 = txtUnitNo2.Text;
            _company.PostCode = txtPostCode.Text;
            _company.Street = txtStreet.Text;
            _company.City = txtCity.Text;
            _company.Country = ddlCountry.Text;
            _company.CompanyColorCodeRGB = txtRed.Text + "," + txtGreen.Text + "," + txtBlue.Text;
            _company.CompanyColorCode1 = txtHex.Text;

            if (_updatflag)
                _company.Status = "UPDATE";
            else
                _company.Status = "NEW";

            _company.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

            long _result = _companydb.Insert(_company);

            if (_result > 0)
            {
                bool _servicesUpdate = false;
                ProviderCompanyServices _pservices = new ProviderCompanyServices();
                ProviderCompanyServicesDB _pservicesdb = new ProviderCompanyServicesDB();
                foreach (ListItem lstItem in cblProviderType.Items)
                {
                    _pservices.ProviderCompanyId = _result;
                    _pservices.ProviderTypeId = Convert.ToInt32(lstItem.Value);
                    if (lstItem.Selected)
                    {
                        //lblCheckList.Text += lstItem.Text + " >> " + lstItem.Value + " ";
                        _pservices.Status = true;
                        //lblCheckList.Text += _protypedb.AssignProviderType(_protype).ToString() + " <br />";
                    }
                    else
                    {
                        _pservices.Status = false;
                    }
                    _pservices.UpdateFlag = _updatflag;
                    if (_pservicesdb.Insert(_pservices) > 0)
                    {
                        _servicesUpdate = true;
                    }
                }

                lblProviderCompanyId.Text = _result.ToString();

                if (_updatflag)
                {
                    lblMessage.Text = "Updating Provider Company is succeeded!";
                    //btnAddNew.Text = "Update";
                }
                else if (_updatflag == false)
                {
                    lblMessage.Text = "Adding New Provider Company is succeeded!";
                }

                if (_servicesUpdate)
                {
                    lblMessage.Text += "<br />Add/Update Provider type(s) list is created successfully.";
                }
                else
                {
                    lblMessage.Text += "<br />Add/Update Provider type(s) list failed.";
                }
            }
            else
            {
                lblMessage.Text = "Insert/Update Provider Company failed.";
            }

        }

    }
    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
    }

    protected void ClearForm()
    {
        txtCompanyName.Text = "";
        ddlBusIndType.SelectedIndex = 0;
        ddlCompanyType.SelectedIndex = 0;

        txtPhoneNo1.Text = "";
        txtPhoneNo2.Text = "";
        txtFaxNo1.Text = "";
        txtFaxNo2.Text = "";
        txtEmail1.Text = "";
        txtEmail2.Text = "";
        txtURL1.Text = "";
        txtURL2.Text = "";
        txtUnitNo1.Text = "";
        txtUnitNo2.Text = "";
        txtPostCode.Text = "";
        txtStreet.Text = "";
        txtCity.Text = "";
        ddlCountry.SelectedIndex = 0;
        lblProviderCompanyId.Text = "";
        foreach (ListItem lstItem in cblProviderType.Items)
        {
            lstItem.Selected = false;
        }
        lblMessage.Text = "";
        lblSearchMessage.Text = "";
        //btnAddNew.Text = "Submit";
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCompany.aspx");
    }

    protected void ShowCompanyInformation(Company CompanyInfo)
    {
        ddlBusIndType.Text = CompanyInfo.BusIndType;
        ddlCompanyType.Text = CompanyInfo.CompanyType;
        txtPhoneNo1.Text = CompanyInfo.PhoneNo1;
        txtPhoneNo2.Text = CompanyInfo.PhoneNo2;
        txtEmail1.Text = CompanyInfo.Email1;
        txtEmail2.Text = CompanyInfo.Email2;
        txtURL1.Text = CompanyInfo.URL1;
        txtURL2.Text = CompanyInfo.URL2;
        txtUnitNo1.Text = CompanyInfo.UnitNo1;
        txtUnitNo2.Text = CompanyInfo.UnitNo2;
        txtFaxNo1.Text = CompanyInfo.FaxNo1;
        txtFaxNo2.Text = CompanyInfo.FaxNo2;
        txtPostCode.Text = CompanyInfo.PostCode;
        txtStreet.Text = CompanyInfo.Street;
        txtCity.Text = CompanyInfo.City;
        ddlCountry.Text = CompanyInfo.Country;
        if (CompanyInfo.CompanyColorCodeRGB.Trim() != string.Empty)
        {
            string[] arrColor = CompanyInfo.CompanyColorCodeRGB.Split(',');
            txtRed.Text = arrColor[0];
            txtGreen.Text = arrColor[1];
            txtBlue.Text = arrColor[2];
        }
        else
        {
            txtRed.Text = "255";
            txtGreen.Text = "255";
            txtBlue.Text = "255";
        }
    }

    private void ShowCompanyServices(ProviderCompanyServicesList pserviceslist)
    {
        foreach (ListItem lstItem in cblProviderType.Items)
        {
            lstItem.Selected = false;
        }

        if (pserviceslist != null)
        {
            foreach (ProviderCompanyServices ps in pserviceslist)
            {
                foreach (ListItem lstItem in cblProviderType.Items)
                {
                    if (ps.ProviderTypeId.ToString() == lstItem.Value)
                    {
                        lstItem.Selected = true;
                        break;
                    }
                }
            }
        }


    }

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        if (txtCompanyName.Text.Trim() != null)
        {
            string strCompanyName = txtCompanyName.Text;
            ClearForm();
            txtCompanyName.Text = strCompanyName;
            GetCompanyInformation(txtCompanyName.Text.Trim());
        }
    }

    protected void btnViewList_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCompany.aspx");
    }

    private void GetCompanyInformationById(Int64 ProviderCompanyId)
    {
        lblProviderCompanyId.Text = ProviderCompanyId.ToString();
        Company _company = new Company();
        CompanyDB _companydb = new CompanyDB();
        _company = _companydb.GetCompany(ProviderCompanyId);
        if (_company != null)
        {
            txtCompanyName.Text = _company.CompanyName;
            ShowCompanyInformation(_company);
            ProviderCompanyServicesList _pserviceslist = new ProviderCompanyServicesList();
            ProviderCompanyServicesDB _pservicesdb = new ProviderCompanyServicesDB();
            _pserviceslist = _pservicesdb.GetProviderServicesList(ProviderCompanyId);

            ShowCompanyServices(_pserviceslist);
            //btnAddNew.Text = "Update";
        }
    }

    private void GetCompanyInformation(string CompanyName)
    {
        lblProviderCompanyId.Text = string.Empty;
        Int64 _result = 0;
        CompanyDB _companydb = new CompanyDB();
        _result = _companydb.CheckCompanyName(CompanyName);
        if (_result > 0)
        {
            lblProviderCompanyId.Text = _result.ToString();
            Company _company = new Company();
            _company = _companydb.GetCompany(_result);
            ShowCompanyInformation(_company);
            ProviderCompanyServicesList _pserviceslist = new ProviderCompanyServicesList();
            ProviderCompanyServicesDB _pservicesdb = new ProviderCompanyServicesDB();
            _pserviceslist = _pservicesdb.GetProviderServicesList(_result);

            ShowCompanyServices(_pserviceslist);

            //btnAddNew.Text = "Update";
        }
        else
        {
            lblSearchMessage.Text = "Company Name is not found!";
        }
    }
    protected void txtCompanyName_TextChanged(object sender, EventArgs e)
    {
        GetCompanyInformation(txtCompanyName.Text);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddNewProvider.aspx");
    }
}
